﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Data.SqlClient;

namespace MMOG
{
    public class OfflineMessage
    {
        public String from;
        public String msg;

        public OfflineMessage(String from, String msg)
        {
            this.from = from;
            this.msg = msg;
        }
    }

    public class Account
    {
        public String username;
        public String password;

        public Account(String username, String password)
        {
            this.username = username;
            this.password = password;
        }
    }

    public static class Database
    {
        public static string serverName = "AIDA\\SQLEXPRESS";
        public static SqlConnection connection = null;

        public static int init()
        {
            try
            {
                connection = new SqlConnection("Server=" + serverName + ";Database=Versus;Integrated Security=True");
                connection.Open();
                return 0;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        public static Account authenticate(String username, String password)
        {
            try
            {
                String commandData = "SELECT COUNT(*) FROM Account WHERE Username='"
                    + username + "' AND Password='" + password + "'";
                SqlCommand command = new SqlCommand(commandData, connection);
                SqlDataReader dtReader = command.ExecuteReader();
                if (dtReader.Read())
                {
                    int count = dtReader.GetInt32(0);
                    dtReader.Close();
                    if (count == 1) return new Account(username, password);
                }
            }
            catch (Exception) {}
            return null;
        }

        public static int addBugReport(String username, String msg)
        {
            try
            {
                String commandData = "INSERT INTO BugReport(Username,Report) VALUES ('"
                    + username + "','" + msg + "')";
                SqlCommand command = new SqlCommand(commandData, connection);
                command.ExecuteNonQuery();
            }
            catch (Exception) {}
            return 0;
        }

        public static List<OfflineMessage> getOfflineMessage(String username)
        {
            List<OfflineMessage> result = new List<OfflineMessage>();
            try
            {
                String commandData = "SELECT Sender, Contents FROM Message WHERE Recipient='"
                    + username + "' AND Status=0";
                SqlCommand command = new SqlCommand(commandData, connection);
                SqlDataReader dtReader = command.ExecuteReader();
                while (dtReader.Read())
                {
                    String from = dtReader.GetString(0);
                    String content = dtReader.GetString(1);
                    result.Add(new OfflineMessage(from, content));
                }
                dtReader.Close();
            }
            catch (Exception) { }
            return result;
        }

        public static void close()
        {
            if (connection != null)
            {
                try
                {
                    connection.Close();
                }
                catch (Exception)
                {
                }
            }
        }
    }
}
